package com.xw.controller.example;

import com.alibaba.fastjson.JSONObject;
import com.xw.common.Const;
import com.xw.model.sdk.AliPayModel;
import com.xw.model.sdk.WeChatModel;
import com.xw.service.tool.alipay.AlipayService;
import com.xw.service.tool.wechat.WeChatService;
import com.xw.util.AppUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 支付测试案例
 * app支付
 */
@Controller
@RequestMapping("/payTest")
@Scope("prototype")
public class PayTest {
    @Resource
    private AlipayService alipayService;
    @Resource
    private WeChatService weChatService;

    /**
     * 支付宝
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/toPay",method = RequestMethod.POST)
    @ResponseBody
    public Object pay(HttpServletRequest request) throws Exception{
        AliPayModel model = new AliPayModel();
        model.setBody("我是测试数据");
        model.setSubject("App支付测试Java");
        model.setOutTradeNo(AppUtil.getOrderCode());
        //支付宝以元为单位
        model.setTotalAmount("0.01");
        model.setNotifyUrl(Const.WEB_URL+"aliPayCallBack/test");
        Map<String, Object> map = alipayService.toPay(model);
        System.out.println(map);
        return JSONObject.toJSON(map);
    }

    /**
     * 微信
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/toWeChatPay",method = RequestMethod.POST)
    @ResponseBody
    public Object toWeChatPay(HttpServletRequest request) throws Exception{
        WeChatModel weChatModel = new WeChatModel();
        weChatModel.setBody("test");
        //微信以分为单位
        weChatModel.setTotal_fee(1);
        weChatModel.setNotify_url(Const.WEB_URL+"weChatCallBack/test");
        weChatModel.setOut_trade_no(AppUtil.getOrderCode());

        Map<String, Object> map = weChatService.toPay(weChatModel,request);
        return JSONObject.toJSON(map);
    }

}
